package com.tgy.controller;


import com.tgy.entity.Books;
import com.tgy.service.BooksService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("book")
public class BooksController {

/*    @Autowired
    @Qualifier("BooksServiceImpl")*/
    @Resource
    private BooksService booksService;

    @RequestMapping("aa")
    public String aa(Model model) {

        model.addAttribute("msg", booksService.getAll().toString());
        return "allBook";
    }

    @RequestMapping("allBook")
    public String allbook(Model model) {

        List<Books> list = booksService.getAll();
        model.addAttribute("list", list);
        return "allBook";
    }


    @RequestMapping("toAddBook")
    public String toAddBook() {
        return "addbook";
    }

    @RequestMapping("addBook")
    public String addBook(Books books) {
        this.booksService.add(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("del/{bookid}")
    public String delBook(@PathVariable("bookid") int id) {
        this.booksService.del(id);
        return "redirect:/book/allBook";
    }



    @RequestMapping("toUpdateBook")
    public String toUpdateBook(Model model,int id) {
        model.addAttribute("book",this.booksService.getBookById(id));
        return "updateBook";
    }

    @RequestMapping("query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Books books) {
        HashMap<String, Object> hMap = new HashMap<>();
        hMap.put("code","success");
        hMap.put("result",this.booksService.query(books));
        return hMap;
    }
}

